package com.mylove.judas.yaya.common;

import com.mylove.judas.yaya.entity.FragmentInfo;
import com.mylove.judas.yaya.ui.fragment.DYTCFragment;
import com.mylove.judas.yaya.ui.fragment.GIFFragment;
import com.mylove.judas.yaya.ui.fragment.MRMJFragment;
import com.mylove.judas.yaya.ui.fragment.MyPublishFragment;
import com.mylove.judas.yaya.ui.fragment.OriginalFragment;
import com.mylove.judas.yaya.ui.fragment.RecommendFragment;
import com.mylove.judas.yaya.ui.fragment.base.BaseFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Judas on 2018/5/3.
 */

public class FragmentFactory {

    private static final String TYPE_ALL = "全部";
    private static final String TYPE_TEXT = "文字";
    private static final String TYPE_IMAGE = "图片";
    private static final String TYPE_GIF = "GIF图";
    private static final String TYPE_VIDEO = "视频";
    private static final String TYPE_DEFAULT = "美图";

    private static final int TYPE_ALL_INT = 0;
    private static final int TYPE_TEXT_INT = 1;
    private static final int TYPE_IMAGE_INT = 2;
    private static final int TYPE_GIF_INT = 3;
    private static final int TYPE_VIDEO_INT = 4;

    private static Map<Integer, FragmentInfo> map = new HashMap<>();


    public static FragmentInfo createFragmentInfo(int position){

        FragmentInfo fi = map.get(position);


//        fragments.add(new FragmentInfo("全部", RecommendFragment.class));
//        fragments.add(new FragmentInfo("文字", MRMJFragment.class));
//        fragments.add(new FragmentInfo("图片", DYTCFragment.class));
//        fragments.add(new FragmentInfo("视频", OriginalFragment.class));
//        fragments.add(new FragmentInfo("我的发布", MyPublishFragment.class));

        if(fi == null){
            switch (position){
                case TYPE_ALL_INT:
                    fi = new FragmentInfo(TYPE_ALL, RecommendFragment.class);
                    break;
                case TYPE_TEXT_INT:
                    fi = new FragmentInfo(TYPE_TEXT, MRMJFragment.class);
                    break;
                case TYPE_IMAGE_INT:
                    fi = new FragmentInfo(TYPE_IMAGE, DYTCFragment.class);
                    break;
                case TYPE_GIF_INT:
                    fi = new FragmentInfo(TYPE_GIF, GIFFragment.class);
                    break;
                case TYPE_VIDEO_INT:
                    fi = new FragmentInfo(TYPE_VIDEO, OriginalFragment.class);
                    break;
                default:
                    fi = new FragmentInfo(TYPE_DEFAULT, MyPublishFragment.class);
                    break;
            }
        }

        map.put(position, fi);

        return fi;

    }


    private static Map<Integer, BaseFragment> mapFragment = new HashMap<>();

    public static BaseFragment createFragment(FragmentInfo fragmentInfo, int position){

        BaseFragment bf = mapFragment.get(position);

        if(bf == null){

            try {
                bf = (BaseFragment) fragmentInfo.getFragment().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }

        mapFragment.put(position, bf);

        return bf;
    }


}
